#!/usr/bin/env bash

# REQUIRES: preload,make,fortran
# RUN: bash %s %T/fortran
# RUN: cd %T/fortran; %{intercept-build} --cdb preload.json make -C src
# RUN: cd %T/fortran; %{cdb_diff} preload.json expected.json

set -o errexit
set -o nounset
set -o xtrace

# the test creates a subdirectory inside output dir.
#
# ${root_dir}
# ├── expected.json
# └── src
#    ├── Makefile
#    └── main.c

root_dir=$1
mkdir -p "${root_dir}/src"

cat > "${root_dir}/src/ex.f95" << EOF
! Test Program
program first
print *,'This is  my first program'
end program first
EOF

cat > ${root_dir}/src/Makefile << EOF
all:
	gfortran ex.f95 -o ex
EOF

cat > "${root_dir}/expected.json" << EOF
[
    {
        "arguments": [
            "gfortran",
            "-c",
            "-o",
            "ex",
            "ex.f95"
        ],
        "directory": "${root_dir}/src",
        "file": "ex.f95"
    }
]
EOF
